<?php
/*
---------------------------------------------------------------------------
admin/editsettings.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
require("../../library/frontend.php");

page_start('Settings', TRUE);
if (isset($_POST['time_zone'])) {
	$settings_data = "<?php\n";
	$settings_data .= '$time_zone = "' . $_POST['time_zone'] . '";';
	$settings_data .= '$from_email = "' . $_POST['from_email'] . '";';
	$settings_data .= '$to_email = "' . $_POST['to_email'] . '";';
	$settings_data .= '$dbhost = "' . $_POST['dbhost'] . '";';
	$settings_data .= '$dbname = "' . $_POST['dbname'] . '";';
	$settings_data .= '$dbuser = "' . $_POST['dbuser'] . '";';
	$settings_data .= '$dbpass = "' . $_POST['dbpass'] . '";';
	$settings_data .= '$keep_data = "' . $_POST['keep_data'] . '";';
	$settings_data .= '$keep_ratio = "' . $_POST['keep_ratio'] . '";';
	$settings_data .= '$refresh_frequency = "' . $_POST['refresh_frequency'] . '";';
	$settings_data .= '$install_dir = "' . $_POST['install_dir'] . '";';
	$settings_data .= '$email_start = "' . $_POST['email_start'] . '";';
	$settings_data .= '$email_end = "' . $_POST['email_end'] . '";';
	$settings_data .= '$wmi_user = "' . $_POST['wmi_user'] . '";';
	$settings_data .= '$wmi_password = "' . $_POST['wmi_password'] . '";';
	$settings_file = "../../library/settings.php";
	if (is_writable($settings_file)){
		if (!$handle = fopen($settings_file, 'w'))
			echo 'Cannot open file ', $settings_file;
		elseif (fwrite($handle, $settings_data) === FALSE)
			echo 'Cannot write to file ', $settings_file;
		else
			echo 'Settings Saved Successfully! <a href="editsettings.php">Back to settings</a>';
		fclose($handle);
	}
	else
		echo $settings_file, ' is not writeable';
}
else {
	require("../../library/timezones.php");
	echo '<form method="post" action="editsettings.php" enctype="multipart/form-data">
	<table>
	<tr><th colspan=2>Time:</th></tr>
	<tr><td align="right">Time Zone:</td>
	<td><select name="time_zone">';
	foreach ($tz as $value => $name) {
		echo '<option value="', $value, '"';
		if ($value == $time_zone)
			echo ' selected="selected"';
		echo '>', $name, '</option>';
	}
	echo '</select></td></tr>
	<tr><th colspan=2>Email Settings:</th></tr>
	<tr><td align="right">From Email Address:</td><td><input type="text" size="35" name="from_email" value="', $from_email, '" /></td></tr>
	<tr><td align="right">To Email Address:</td><td><input type="text" size="35" name="to_email" value="', $to_email, '" /> Separate multiple addresses with a comma</td></tr>
	<tr><td aligh="right">Only Email New Alert Notifications between:</td><td><select name="email_start">';
	for ($i = 0; $i <= 24; $i++) {
		echo '<option value="', $i, '"';
		if ($i == $email_start)
			echo ' selected="selected"';
		echo '>', $i, ':00</option>';
	}
	echo '</select> And <select name="email_end">';
	for ($i = 0; $i <= 24; $i++) {
		echo '<option value="', $i, '"';
		if ($i == $email_end)
			echo ' selected="selected"';
		echo '>', $i, ':00</option>';
	}
	echo '</select> (Changes to existing alerts will still be emailed)</td></tr>
	<tr><th colspan=2>MySQL Settings:</th></tr>
	<tr><td align="right">MySQL Host:</td><td><input type="text" name="dbhost" value="', $dbhost, '" /></td></tr>
	<tr><td align="right">DB Name:</td><td><input type="text" name="dbname" value="', $dbname, '" /></td></tr>
	<tr><td align="right">DB User:</td><td><input type="text" name="dbuser" value="', $dbuser, '" /></td></tr>
	<tr><td align="right">DB Password:</td><td><input type="password" name="dbpass" value="', $dbpass, '" /></td></tr>
	<tr><th colspan=2>Database Maintenance:</th></tr>
	<tr><td align="right">Clean up and Average Data Older Than:</td><td><input type="text" size="3" name="keep_data" value="', $keep_data, '" /> Days</td></tr>
	<tr><td align="right">Average Ratio:</td><td><input type="text" size="3" name="keep_ratio" value="', $keep_ratio, '" /> Records will be averaged down to 1</td></tr>
	<tr><th colspan=2>Microsoft WMI Settings:</th></tr>
	<tr><td align="right">WMI Username(domain/username):</td><td><input type="text" name="wmi_user" value="', $wmi_user, '" /></td></tr>
	<tr><td align="right">WMI Password:</td><td><input type="password" name="wmi_password" value="', $wmi_password, '" /></td></tr>
	<tr><th colspan=2>Misc:</th></tr>
	<tr><td align="right">Auto Refresh Frequency:</td><td><input type="text" size="3" name="refresh_frequency" value="', $refresh_frequency, '" /> Seconds</td></tr>
	<tr><td align="right">SALAM Install Location:</td><td><input type="text" name="install_dir" value="', $install_dir, '" /></td></tr>
	<tr><td colspan="2" align="center"><input name="save" type="submit" class="box" value=" Save "></td></tr>
	</table>
	</form>';
}
page_end();
?>